/**
 * 
 */
package com.hexacta.hrs.web.struts.actions;

import java.util.Date;

import org.vectrics.common.util.DateUtil;
import org.vectrics.employee.Employee;
import org.vectrics.user.Person;

import com.hexacta.hrs.services.crud.CrudService;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.timeoff.AvailableLicenseDaysService;
import com.hexacta.hrs.services.timeoff.TimeOffService;
import com.hexacta.hrs.timeoff.AvailableLicenseDays;
import com.hexacta.hrs.timeoff.Configuration;
import com.hexacta.hrs.timeoff.TimeOff;

/**
 * @author mfunes
 * 
 */
public class TimeOffEmployeeAction extends CrudAbstractAction<TimeOff> {

    private static final long serialVersionUID = 6214440708946388515L;

    private TimeOffService timeOffService;

    private EmployeeService employeeService;

    private Integer consumedStudyDays = 0;

    private AvailableLicenseDaysService availableLicenseDaysService;

    public void setAvailableLicenseDaysService(final AvailableLicenseDaysService availableLicenseDaysService) {
        this.availableLicenseDaysService = availableLicenseDaysService;
    }

    public void setTimeOffService(final TimeOffService timeOffService) {
        this.timeOffService = timeOffService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @Override
    protected CrudService<TimeOff> getCrudService() {
        return timeOffService;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected TimeOff getSearchCriteria() {
        TimeOff criteria = new TimeOff();
        Employee employee = this.obtainEmployee();
        criteria.setEmployee(employee);
        // Date today = DateUtil.getCurrentDate();

        // Bug HRS-871 Se lista de todo el anio
        Date today = new Date();
        today.setYear(DateUtil.getYear(DateUtil.getCurrentDate()) - 1900);
        today.setMonth(11);
        today.setDate(31);
        Date firstDayOfYear = DateUtil.getFirstDayOfCurrentYear();
        criteria.setDateFrom(firstDayOfYear);
        criteria.setDateTo(today);
        this.setConsumedStudyDays(Configuration.getMaxStudyLicenseDaysAllowedRD()
                - this.getAvailableLicenseDays().getStudyLicenseDays());
        return criteria;
    }

    public AvailableLicenseDays getAvailableLicenseDays() {
        Employee employee = this.obtainEmployee();
        return availableLicenseDaysService.getAvailableLicenseDays(employee);
    }

    private Employee obtainEmployee()  {
         Person person = new Person();
         person.setId(getRequestId());
         Employee employee = employeeService.searchByPerson(person);
         
         return employee;
	}

	public void setConsumedStudyDays(Integer consumedStudyDays) {
        this.consumedStudyDays = consumedStudyDays;
    }

    public Integer getConsumedStudyDays() {
        return consumedStudyDays;
    }
}
